package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import java.util.GregorianCalendar;

import com.lb.jwitter.service.ConditionReport; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.util.Message; 
import java.util.Collections; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import java.util.List; 
import java.util.Map; 

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.lb.jwitter.util.ApplicationContextProvider; 
 import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblCanBoAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    private int page; 
    public int getPage() { return page; }

    private int pages; 
    public int getPages() { return pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private String idList; 
    public String getIdList() { return idList; } 
    public void setIdList(String idList) { this.idList = idList; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public Long getId() {
       return id;
 }

    public void setId(Long id) {
       this.id = id;
 }

    private String ten; 
    public void setTen(String ten) {
       this.ten = ten;
 }

    public String getTen() {
       return ten;
 }

    private String ghiChu; 
    public void setGhiChu(String ghiChu) {
       this.ghiChu = ghiChu;
 }

    public String getGhiChu() {
       return ghiChu;
 }

    private String maQuanLy; 
    public void setMaQuanLy(String maQuanLy) {
       this.maQuanLy = maQuanLy;
 }

    public String getMaQuanLy() {
       return maQuanLy;
 }

    private String ngaySinh; 
    public void setNgaySinh(String ngaySinh) {
       this.ngaySinh = ngaySinh;
 }

    public String getNgaySinh() {
       return ngaySinh;
 }

    private String noiSinh; 
    public void setNoiSinh(String noiSinh) {
       this.noiSinh = noiSinh;
 }

    public String getNoiSinh() {
       return noiSinh;
 }

    private String diaChi; 
    public void setDiaChi(String diaChi) {
       this.diaChi = diaChi;
 }

    public String getDiaChi() {
       return diaChi;
 }

    private String dienThoai; 
    public void setDienThoai(String dienThoai) {
       this.dienThoai = dienThoai;
 }

    public String getDienThoai() {
       return dienThoai;
 }

    private String trinhDo; 
    public void setTrinhDo(String trinhDo) {
       this.trinhDo = trinhDo;
 }

    public String getTrinhDo() {
       return trinhDo;
 }

    private String ngheNghiep; 
    public void setNgheNghiep(String ngheNghiep) {
       this.ngheNghiep = ngheNghiep;
 }

    public String getNgheNghiep() {
       return ngheNghiep;
 }

    private String noiLam; 
    public void setNoiLam(String noiLam) {
       this.noiLam = noiLam;
 }

    public String getNoiLam() {
       return noiLam;
 }

    private TblCanBo tblCanBo; 
    public TblCanBo getTblCanBo () {
        return this.tblCanBo ;
 }

    private Collection<TblCanBo> tblCanBos; 
    public Collection<TblCanBo> getTblCanBos() {
        return this.tblCanBos; 
      } 

    private TblCanBoService tblCanBoService; 

	private int preUpdate(Message msg, TblCanBo obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblCanBo obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String update() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CanBo", "sua")){ 
        return "noprivilege"; 
    }           tblCanBoService = (TblCanBoService)context.getBean("tbl_CanBoClient"); 
        msg = new Message(); 
        tblCanBo = tblCanBoService.gettblCanBo(new Long(id)); 
        tblCanBo.setTen(ten); 
        tblCanBo.setGhiChu(ghiChu); 
        tblCanBo.setMaQuanLy(maQuanLy); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       try{
	       Date dd = df.parse(ngaySinh); 
	       GregorianCalendar c = new GregorianCalendar(); 
	       c.setTime(dd);
	       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
	       tblCanBo.setNgaySinh(d); 
       }catch(Exception e){
    	   e.printStackTrace();
       }
        tblCanBo.setNoiSinh(noiSinh); 
        tblCanBo.setDiaChi(diaChi); 
        tblCanBo.setDienThoai(dienThoai); 
        tblCanBo.setTrinhDo(trinhDo); 
        tblCanBo.setNgheNghiep(ngheNghiep); 
        tblCanBo.setNoiLam(noiLam); 
        preUpdate(msg,tblCanBo); 
       if (msg.isPreMessage_b()) 
       { 
           tblCanBoService.updatetblCanBo(tblCanBo); 
           postUpdate(msg, tblCanBo); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblCanBos =       tblCanBoService.findAlltblCanBo(pageSize, page); 
      pages = tblCanBoService.counttblCanBoByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&ten="; 
      dataString += "&ghiChu="; 
      dataString += "&maQuanLy="; 
      dataString += "&ngaySinh="; 
      dataString += "&noiSinh="; 
      dataString += "&diaChi="; 
      dataString += "&dienThoai="; 
      dataString += "&trinhDo="; 
      dataString += "&ngheNghiep="; 
      dataString += "&noiLam="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_CanBo Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CanBo", "xem")){ 
        return "noprivilege"; 
    }           tblCanBoService = (TblCanBoService)context.getBean("tbl_CanBoClient"); 
       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        tblCanBos =        tblCanBoService.findAlltblCanBo(pageSize, page); 
        pages = tblCanBoService.counttblCanBoByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&ten="; 
      dataString += "&ghiChu="; 
      dataString += "&maQuanLy="; 
      dataString += "&ngaySinh="; 
      dataString += "&noiSinh="; 
      dataString += "&diaChi="; 
      dataString += "&dienThoai="; 
      dataString += "&trinhDo="; 
      dataString += "&ngheNghiep="; 
      dataString += "&noiLam="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_CanBo Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
  public String delete() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CanBo", "xoa")){ 
        return "noprivilege"; 
    }           tblCanBoService = (TblCanBoService)context.getBean("tbl_CanBoClient"); 
    msg = new Message(); 

    List<String> ids = new ArrayList<String>(); 
    Collections.addAll(ids, idList.split(",")); 
    for(String id : ids){ 
        tblCanBoService.deleteListtblCanBo(ids); 
    } 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblCanBos =       tblCanBoService.findAlltblCanBo(pageSize, page); 
      pages = tblCanBoService.counttblCanBoByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&ten="; 
      dataString += "&ghiChu="; 
      dataString += "&maQuanLy="; 
      dataString += "&ngaySinh="; 
      dataString += "&noiSinh="; 
      dataString += "&diaChi="; 
      dataString += "&dienThoai="; 
      dataString += "&trinhDo="; 
      dataString += "&ngheNghiep="; 
      dataString += "&noiLam="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Delete tbl_CanBo Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 	private int preInsert(Message msg, TblCanBo obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblCanBo obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String insert() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CanBo", "them")){ 
        return "noprivilege"; 
    }           tblCanBoService = (TblCanBoService)context.getBean("tbl_CanBoClient"); 
        msg = new Message(); 
        tblCanBo = new TblCanBo(); 
        tblCanBo.setTen(ten); 
        tblCanBo.setGhiChu(ghiChu); 
        tblCanBo.setMaQuanLy(maQuanLy); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       try{
	       Date dd = df.parse(ngaySinh); 
	       GregorianCalendar c = new GregorianCalendar(); 
	       c.setTime(dd);
	       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
	       tblCanBo.setNgaySinh(d); 
       }catch(Exception e){
    	   e.printStackTrace();
       }
        tblCanBo.setNoiSinh(noiSinh); 
        tblCanBo.setDiaChi(diaChi); 
        tblCanBo.setDienThoai(dienThoai); 
        tblCanBo.setTrinhDo(trinhDo); 
        tblCanBo.setNgheNghiep(ngheNghiep); 
        tblCanBo.setNoiLam(noiLam); 
        preInsert(msg,tblCanBo); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblCanBoService.createtblCanBo(tblCanBo); 
           postInsert(msg, tblCanBo); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblCanBo.setId(id); 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblCanBos =       tblCanBoService.findAlltblCanBo(pageSize, page); 
      pages = tblCanBoService.counttblCanBoByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&ten="; 
      dataString += "&ghiChu="; 
      dataString += "&maQuanLy="; 
      dataString += "&ngaySinh="; 
      dataString += "&noiSinh="; 
      dataString += "&diaChi="; 
      dataString += "&dienThoai="; 
      dataString += "&trinhDo="; 
      dataString += "&ngheNghiep="; 
      dataString += "&noiLam="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_CanBo Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 	private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String search() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CanBo", "xem")){ 
        return "noprivilege"; 
    }           tblCanBoService = (TblCanBoService)context.getBean("tbl_CanBoClient"); 
        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
    if(id!= null && !id.equals(0)){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("id");
        con.setValue1(new Long(id));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(ten != null && !ten.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("ten");
        con.setValue1("%" + ten + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(ghiChu != null && !ghiChu.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("ghiChu");
        con.setValue1("%" + ghiChu + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(maQuanLy != null && !maQuanLy.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("maQuanLy");
        con.setValue1("%" + maQuanLy + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(ngaySinh != null && !ngaySinh.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("ngaySinh");
        con.setValue1(ngaySinh);
        con.setOperator("eq");
        con.setType("date");
        cons.add(con);	
    } 
    if(noiSinh != null && !noiSinh.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("noiSinh");
        con.setValue1("%" + noiSinh + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(diaChi != null && !diaChi.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("diaChi");
        con.setValue1("%" + diaChi + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(dienThoai != null && !dienThoai.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("dienThoai");
        con.setValue1("%" + dienThoai + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(trinhDo != null && !trinhDo.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("trinhDo");
        con.setValue1("%" + trinhDo + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(ngheNghiep != null && !ngheNghiep.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("ngheNghiep");
        con.setValue1("%" + ngheNghiep + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(noiLam != null && !noiLam.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("noiLam");
        con.setValue1("%" + noiLam + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
        preSearch(msg, cons); 
       if (msg.isPreMessage_b()) 
       { 
           if(pageSize == 0){ 
               pageSize = 20; 
           } 
            if(page == 0){ 
               page =1; 
           } 
           tblCanBos = tblCanBoService.findtblCanBoByCondition(cons,pageSize,page); 
           pages = tblCanBoService.counttblCanBoByCondition(cons)/pageSize + 1; 
           postSearch(msg, cons); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
      dataString = ""; 
      dataString += "&id=" + id; 
      dataString += "&ten=" + ten; 
      dataString += "&ghiChu=" + ghiChu; 
      dataString += "&maQuanLy=" + maQuanLy; 
      dataString += "&ngaySinh=" + ngaySinh; 
      dataString += "&noiSinh=" + noiSinh; 
      dataString += "&diaChi=" + diaChi; 
      dataString += "&dienThoai=" + dienThoai; 
      dataString += "&trinhDo=" + trinhDo; 
      dataString += "&ngheNghiep=" + ngheNghiep; 
      dataString += "&noiLam=" + noiLam; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Seach tbl_CanBo Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String editForm() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CanBo", "sua")){ 
        return "noprivilege"; 
    }           tblCanBoService = (TblCanBoService)context.getBean("tbl_CanBoClient"); 
        tblCanBo = tblCanBoService.gettblCanBo(new Long(id)); 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi EditForm tbl_CanBo Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 } 
 
